package cn.hl.hm.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.dao.VehicleLengthMapper;
import cn.hl.hm.dao.VtVlMapper;
import cn.hl.hm.entity.Route;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.VehicleLength;
import cn.hl.hm.entity.VehicleType;
//import cn.hl.hm.entity.VtVl;
import cn.hl.hm.service.RouteService;
import cn.hl.hm.service.RouteVehiclePriceService;
import cn.hl.hm.service.VehicleTypeService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.util.BeanPicUtil;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

/**
 * 车型业务
 * @author LBJ
 *
 */
@Controller
@RequestMapping("/vehicleTypeController")
public class VehicleTypeController {
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	VehicleTypeService  vehicleTypeService;
	@Autowired
	RouteService routeService;
    @Autowired
    private BeanPicUtil beanPicUtil;
	@Autowired
	RouteVehiclePriceService rvpService;
	@Autowired
	VtVlMapper vtVlMapper;
	@Autowired
	VehicleLengthMapper vehicleLengthMapper;
 
	/**
	 * 当前进行中的订单 1：当前订单列表 2：订单收入列表，3历史订单列表 4 所有订单列表
	 * @param driverId
	 * @param pageId
	 * @param pageCount
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectVehicleTypeList", method = RequestMethod.POST)
	public String selectOrderList(  @RequestParam Integer pageId,
	        @RequestParam Integer pageCount,
	        String vehicleName,
	        String id,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("车型管理---车型列表---vehicleTypeController/selectVehicleTypeList");
		request.setCharacterEncoding("UTF-8");
		try {
			AppPager handle = AppPager.handle(pageId, pageCount);
			VehicleType param = new VehicleType();
			param.setPageId(handle.getPageId());
			param.setPageCount(handle.getPageCount());
			param.setName(vehicleName);
			param.setStatus("0");
			if(id!=null && !"".equals(id)){
				param.setId(Integer.parseInt(id));
			}
			List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleType(param);
			List<VehicleType> vehicleTypeListForBack = new ArrayList<>();
			for (VehicleType vehicleType : vehicleTypeList) {
				vehicleType = beanPicUtil.modifyUrl(vehicleType);
				vehicleTypeListForBack.add(vehicleType);
			}
		 
			Result.ok(vehicleTypeListForBack, InfoCode.SUCCESS, response);
		} catch (Exception e) {
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
//	 /**
//	    * @Title: send  
//	    * @Description:添加车型  =====旧
//	    * @date: 2017年1月5日
//	    * @author: 星矢
//	    * @throws
//	   */
//		@RequestMapping(value = "/addVehicleType", method = RequestMethod.POST)
//		public String addVehicleType(
//				VehicleType vehicleType,
//			HttpServletRequest request, HttpServletResponse response)  {
////			TBackstageUserBean ue = (TBackstageUserBean) request.getSession().getAttribute("BACKSTAGEUSER");
////			 
////				if (ue != null) {
////					if(ue.gettBackstageUserEntity()!=null){
////						if (ue.gettBackstageUserEntity().getMailbox() != null || !"".equals(ue.gettBackstageUserEntity().getMailbox())) {
////							reviewUserName = ue.gettBackstageUserEntity().getMailbox();
////						} else {
////							reviewUserName = "";
////						}
////					}
////				}
//	 
//			 
//			try{
//				vehicleType.setCreateTime(new Date());
//				vehicleType.setStatus("0");
//				vehicleType = beanPicUtil.cutUrl(vehicleType);
//				int result = vehicleTypeService.addVehicleType(vehicleType) ; // 添加车型
//				if(result>0){
//						Result.ok(null, InfoCode.SUCCESS, response);
//						return null;
//					}else {
//						Result.ok(null, InfoCode.FAILURE, response);
//						return null;
//					
//				}
//			}catch(Exception e){
//				e.printStackTrace();
//				Result.ok(null, InfoCode.FAILURE, response);
//				return null;
//			}
//			
//		}
	
	
	 /**
	    * @Title: send  
	    * @Description:添加车型  =====新  添加车型后，需要关联所有的车长 关联所有的路线
	    * @date: 2017年1月5日
	    * @author: 星矢
	    * @throws
	   */
		@RequestMapping(value = "/addVehicleType", method = RequestMethod.POST)
		public String addVehicleType(
				VehicleType vehicleType,
			HttpServletRequest request, HttpServletResponse response)  {
			logger.info("车型管理---添加车型---vehicleTypeController/addVehicleType");
//			TBackstageUserBean ue = (TBackstageUserBean) request.getSession().getAttribute("BACKSTAGEUSER");
//			 
//				if (ue != null) {
//					if(ue.gettBackstageUserEntity()!=null){
//						if (ue.gettBackstageUserEntity().getMailbox() != null || !"".equals(ue.gettBackstageUserEntity().getMailbox())) {
//							reviewUserName = ue.gettBackstageUserEntity().getMailbox();
//						} else {
//							reviewUserName = "";
//						}
//					}
//				}
	 
			 
			try{
				vehicleType.setCreateTime(new Date());
				vehicleType.setStatus("0");
				vehicleType = beanPicUtil.cutUrl(vehicleType);
				int result = vehicleTypeService.addVehicleType(vehicleType) ; // 添加车型
				if(result>0){
					 
//					Route r = new Route();
//					r.setRouteStatus("0");
//					VehicleType param = new VehicleType();
//					param.setStatus("0");
//					List<Route> routeList = routeService.selectRoute(r);
//					
//					List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleType(param);
//					
//					List<VehicleLength> vehicleLengthList = vehicleLengthMapper.selectVehicleLength();
//					Route r = new Route();
//					r.setRouteStatus("0");
//					List<Route> route = routeService.selectRoute(r);
//					for (VehicleLength vehicleLength : vehicleLengthList) {// 添加车型后，需要关联所有的车长
//						 VtVl vtvl = new VtVl();
//						 vtvl.setVehicleLengthId(vehicleLength.getId());
//						 vtvl.setVehicleTypeId(vehicleType.getId());
//						 vtvl.setCreateTime(new Date().toLocaleString());
//						 vtVlMapper.insertSelective(vtvl);
//						// 增加一个车型，则所有路线都增加该车型double
//							for (Route route2 : route) {  //添加车型后，需要关联所有的路线
//								RouteVehiclePrice rvp = new RouteVehiclePrice();
//								rvp.setPrice(new BigDecimal(0));
//								rvp.setRouteId(route2.getId());
//								rvp.setDateTime(new Date().toLocaleString());
//								rvp.setVtVlId(vtvl.getId());
//								rvpService.addRouteVehiclePrice(rvp);
//							}
//					}
						Result.ok(null, InfoCode.SUCCESS, response);
						return null;
					}else {
						Result.ok(null, InfoCode.FAILURE, response);
						return null;
					
				}
			}catch(Exception e){
				e.printStackTrace();
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
			
		}
		
		
		 /**
		    * @Title: send  
		    * @Description:修改车型
		    * @date: 2017年1月5日
		    * @author: 星矢
		    * @throws
		   */
			@RequestMapping(value = "/updateVehicleType", method = RequestMethod.POST)
			public String updateVehicleType(
					VehicleType vehicleType,
				HttpServletRequest request, HttpServletResponse response)  {
				logger.info("车型管理---修改车型---vehicleTypeController/updateVehicleType");
//				TBackstageUserBean ue = (TBackstageUserBean) request.getSession().getAttribute("BACKSTAGEUSER");
//				 
//					if (ue != null) {
//						if(ue.gettBackstageUserEntity()!=null){
//							if (ue.gettBackstageUserEntity().getMailbox() != null || !"".equals(ue.gettBackstageUserEntity().getMailbox())) {
//								reviewUserName = ue.gettBackstageUserEntity().getMailbox();
//							} else {
//								reviewUserName = "";
//							}
//						}
//					}
		 
				/**
				 *  private String name;

	    private Double vehicleLoad;

	    private Double vehicleLength;

	    private Double vehicleWidth;

	    private String vehiclePic;

	    private BigDecimal startingPrice;

	    private Double startingKilometers;

	    private Integer freeWaitingTime;

	    private Double goBeyondStarting;

	    private Double goBeyondFreeWaitingTime;

	    private Date createTime;

	    private Date updateTime;

	    private Byte modelsIdentification;

	    private Byte type;
	    private String status;
				 */
				try{
					vehicleType.setUpdateTime(new Date());
					vehicleType.setStatus("0");
					vehicleType = beanPicUtil.cutUrl(vehicleType);
					int result = vehicleTypeService.updateVehicleType(vehicleType) ; // 添加路线
					if(result>0){
							Result.ok(null, InfoCode.SUCCESS, response);
							return null;
						}else {
							Result.ok(null, InfoCode.FAILURE, response);
							return null;
					}
				}catch(Exception e){
					e.printStackTrace();
					Result.ok(null, InfoCode.FAILURE, response);
					return null;
				}
			}
			 /**
			    * @Title: send  
			    * @Description:删除车型
			    * @date: 2017年1月5日
			    * @author: 星矢
			    * @throws
			   */
				@RequestMapping(value = "/deleteVehicleType", method = RequestMethod.POST)
				public String deleteVehicleType(
						Integer id,
						VehicleType vehicleType,
					HttpServletRequest request, HttpServletResponse response)  {
					logger.info("车型管理---删除车型---vehicleTypeController/deleteVehicleType");
					
					VehicleType asd=vehicleTypeService.selectByPrimaryKey(id);
					
					
					try{
						//int result = vehicleTypeService.deleteVehicleType(id) ; // 添加路线
						
						//VehicleType vehicleType= new VehicleType();
						vehicleType.setName(asd.getName());
						vehicleType.setCreateTime(asd.getCreateTime());
						vehicleType.setFreeWaitingTime(asd.getFreeWaitingTime());
						vehicleType.setGoBeyondFreeWaitingTime(asd.getGoBeyondFreeWaitingTime());
						vehicleType.setGoBeyondStarting(asd.getGoBeyondStarting());
						vehicleType.setModelsIdentification(asd.getModelsIdentification());
						
						vehicleType.setStartingKilometers(asd.getStartingKilometers());
						vehicleType.setStartingPrice(asd.getStartingPrice());
						vehicleType.setType(asd.getType());
						vehicleType.setUpdateTime(asd.getUpdateTime());
						vehicleType.setVehicleLength(asd.getVehicleLength());
						vehicleType.setVehicleLoad(asd.getVehicleLoad());
						vehicleType.setVehiclePic(asd.getVehiclePic());
						vehicleType.setVehicleWidth(asd.getVehicleWidth());
						
						vehicleType.setStatus("1");
						
						int result = vehicleTypeService.updateVehicleType(vehicleType) ;
						if(result>0){
								Result.ok(null, InfoCode.SUCCESS, response);
								return null;
							}else {
								Result.ok(null, InfoCode.FAILURE, response);
								return null;
						}
					}catch(Exception e){
						e.printStackTrace();
						Result.ok(null, InfoCode.FAILURE, response);
						return null;
					}
				}
				
				@RequestMapping(value = "/addalldate", method = RequestMethod.POST)
				public String addalldate(
						HttpServletRequest request, HttpServletResponse response){
 
					VehicleType param = new VehicleType();
					param.setStatus("0");
					List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleType(param);
//					
					List<VehicleLength> vehicleLengthList = vehicleLengthMapper.selectVehicleLength();
					Route r = new Route();
					r.setRouteStatus("0");
//					List<Route> route = routeService.selectRoute(r);
//					for (VehicleType vt : vehicleTypeList) {
//						for (VehicleLength vehicleLength : vehicleLengthList) {// 添加车型后，需要关联所有的车长
//							 VtVl vtvl = new VtVl();
//							 vtvl.setVehicleLengthId(vehicleLength.getId());
//							 vtvl.setVehicleTypeId(vt.getId());
//							 vtvl.setCreateTime(new Date().toLocaleString());
//							 List<VtVl> vv = vtVlMapper.selectVtVlList(vtvl);
//							 if(vv==null || vv.size()==0){
//								 vtVlMapper.insertSelective(vtvl);
//								 // 增加一个车型，则所有路线都增加该车型double
//									for (Route route2 : route) {  //添加车型后，需要关联所有的路线
//										RouteVehiclePrice rvp = new RouteVehiclePrice();
//										rvp.setPrice(new BigDecimal(0));
//										rvp.setRouteId(route2.getId());
//										rvp.setDateTime(new Date().toLocaleString());
//										rvp.setVtVlId(vtvl.getId());
//										rvpService.addRouteVehiclePrice(rvp);
//									}
//							 }
//
//
//						}
//					}
					
						Result.ok(null, InfoCode.SUCCESS, response);
						return null;
				}
}
